package hello.util;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.*;

/**
 * @Description:
 * @Author: m01
 * @Date: created at  2018/12/20  16:31
 * @Modified By:
 */
public final class LogManager {
    private LogManager() {
    }

    public static void main(String[] args) {
        Logger logger = LogManager.getLogger(LogManager.class.getName());
        LogManager.setLogLevel(logger, Level.INFO);
        Handler fileHandlerOfInfo = LogManager.getFileHandler("d:/info_.log", Level.INFO);
        Handler fileHandlerOfWarning = LogManager.getFileHandler("d:/warning_.log", Level.WARNING);
        Handler consoleHandler = LogManager.getConsoleHandler(Level.ALL);
        ArrayList<Handler> handles = new ArrayList<Handler>();
        handles.add(fileHandlerOfInfo);
        handles.add(fileHandlerOfWarning);
        handles.add(consoleHandler);
        LogManager.setHandler(logger, handles);

        logger.info("info");
        logger.warning("warning");


    }

    public static Logger getLogger(String loggerName) {

        Logger log = Logger.getLogger(loggerName);
        return log;
    }

    public static void setLogLevel(Logger logger, Level level) {
        logger.setLevel(level);
    }

    public static void setHandler(Logger logger, List<Handler> handler) {

        for (Handler handler1 : handler) {
            logger.addHandler(handler1);

        }
    }

    public static Handler getFileHandler(String logfileName, final Level level) {
        File file = new File(logfileName);
        System.out.println("lujing"+file.getAbsolutePath());

        if (file.exists()) {
            if (getFileSize(file) > 1) {
                file.delete();
            }
        }
        FileHandler fileHandler = null;
        try {
            fileHandler = new FileHandler(logfileName, true);
            fileHandler.setFormatter(new java.util.logging.Formatter() {
                @Override
                public String format(LogRecord record) {
                    if (record.getLevel().equals(level)) {

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        return format.format(new Date()) + "  " + record.getLevel() + " :" + record.getMessage() + "\n";
                    } else {
                        return "";
                    }

                }
            });
            fileHandler.setLevel(level);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return fileHandler;
        }
    }

    public static Handler getConsoleHandler(Level level) {

        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(level);
        return consoleHandler;
    }

    private static long getFileSize(File file) {

        long size = 0L;
        if (file.exists() && file.isFile()) {
            long length = file.length();
            DecimalFormat decimalFormat = new DecimalFormat("#.00");
            if (length < 1048576) {
                size = 0L;
            } else {
                size = length / 1048576;
            }
        }
        return size;

    }
}
